import {
	login,
	logout,
	getInfo
} from '@/api/user'
import {
	getToken,
	setToken,
	remove,
	getSuper,
	setSuper
} from '@/utils/auth'
import router, {
	resetRouter
} from '@/router'

const state = {
	token: getToken(),
	is_super: getSuper(),
	name: '',
  brand_name: '',
  brand_num: 0,
	avatar: '',
	introduction: '',
	venue: '',
	roles: [],
	xuanList: [],
	xuan: 0
}

const mutations = {
	SET_XUAN: (state, {
		key,
		value
	}) => {
		state[key] = value
	},
	SET_TOKEN: (state, token) => {
		state.token = token
	},
	SET_SUPER: (state, is_super) => {
		state.is_super = is_super
	},
	SET_INTRODUCTION: (state, introduction) => {
		state.introduction = introduction
	},
	SET_NAME: (state, name) => {
		state.name = name
	},
  SET_BRANDNAME: (state, name) => {
    state.brand_name = name
  },
  SET_BRANDNUM: (state, name) => {
    state.brand_num = name
  },
	SET_AVATAR: (state, avatar) => {
		state.avatar = avatar
	},
	SET_ROLES: (state, roles) => {
		state.roles = roles
	}
}

const actions = {
	changeXuan({
		commit
	}, data) {
		commit('SET_XUAN', data)
	},
	// user login
	login({
		commit
	}, userInfo) {
		const {
			username,
			password
		} = userInfo
		return new Promise((resolve, reject) => {
			login({
				username: username,
				password: password
			}).then(response => {
				const {
					data
				} = response
				commit('SET_TOKEN', data.admin_token)
				commit('SET_SUPER', data.is_super)
				setToken(data.admin_token)
				setSuper(data.is_super)
				resolve(data)
			}).catch(error => {
				reject(error)
			})
		})
	},
	getInfo({
		commit,
		state
	}) {
		return new Promise((resolve, reject) => {
			getInfo({
				is_super: state.is_super
			}).then(response => {
				const {
					data
				} = response
				commit('SET_ROLES', data.auth_list)
				commit('SET_NAME', data.un)
				commit('SET_AVATAR', data.brand_logo)
        // commit('SET_AVATAR', data.brand_logo?data.brand_logo:data.cover_img)
        commit('SET_BRANDNAME', data.brand_name)
        commit('SET_BRANDNUM', data.brand_num)
				resolve(data.auth_list)
			}).catch(error => {
				reject(error)
			})
		})
	},
	// user logout
	logout({
		commit,
		state,
		dispatch
	}) {
		return new Promise((resolve, reject) => {
			commit('SET_TOKEN', '')
			commit('SET_ROLES', [])
			remove()
			resetRouter()

			// reset visited views and cached views
			// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
			dispatch('tagsView/delAllViews', null, {
				root: true
			})

			resolve()
		})
	},

	// remove token
	resetToken({
		commit
	}) {
		return new Promise(resolve => {
			commit('SET_TOKEN', '')
			commit('SET_ROLES', [])
			remove()
			resolve()
		})
	},

	// dynamically modify permissions
	// async changeRoles({
	// 	commit,
	// 	dispatch
	// }, role) {
	// 	const token = role + '-token'

	// 	commit('SET_TOKEN', token)
	// 	setToken(token)

	// 	const {
	// 		roles
	// 	} = await dispatch('getInfo')

	// 	resetRouter()

	// 	// generate accessible routes map based on roles
	// 	const accessRoutes = await dispatch('permission/generateRoutes', roles, {
	// 		root: true
	// 	})
	// 	// dynamically add accessible routes
	// 	router.addRoutes(accessRoutes)

	// 	// reset visited views and cached views
	// 	dispatch('tagsView/delAllViews', null, {
	// 		root: true
	// 	})
	// }
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
